feat(mobile): add initialPage prop to Carousel - #864
Conversation
✅ Heimdall Review Status
✅
|
| Code Owner | Status | Calculation | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ui-systems-eng-team |
✅
1/1
|
Denominator calculation
|
327254c to
f3126e3
Compare
f3126e3 to
4422d4f
Compare
4422d4f to
25d7132
Compare
hcopp
left a comment
There was a problem hiding this comment.
Thanks for the PR! Had some feedback
| }); | ||
|
|
||
| const [activePageIndex, setActivePageIndex] = useState(0); | ||
| const [activePageIndex, setActivePageIndex] = useState(() => Math.max(0, initialPage ?? 0)); |
There was a problem hiding this comment.
we should probably use the value from the ref here in case the initialPage prop does in fact change. If that were the case then the activePage state would also change
There was a problem hiding this comment.
@caitlin-coyiuto-cb I think we need to clamp this with the max as well right?
There was a problem hiding this comment.
I don't think we can here on first render since it'd be 0? totalPages gets derived on line 725 later. Also have tests for clamping on last page (if initialPage > last page) and negative initialPage
There was a problem hiding this comment.
Yeah I was wondering if we needed to set activePageIndex when we set hasAppliedInitialPageRef to true. I suppose goToPage does clamp automatically but I wasn't sure if there is an interim side effects if this value was way off.
684136f to
b8a7736
Compare
Add an uncontrolled `initialPage` prop to the cds-mobile and cds-web Carousel so a consumer can open it on a non-zero page with no animation. Seeds `activePageIndex` at init (no `onChangePage` on mount) and performs a one-time, `didInitRef`-guarded non-animated seek on the first successful measurement — reading `initialPage` via a ref and routing through `findNearestLoopOffset` when looping. Adds `initialPage` tests and a story example on both platforms. CB1-2048 Co-Authored-By: Claude <noreply@anthropic.com>
b8a7736 to
45fda23
Compare
hcopp
left a comment
There was a problem hiding this comment.
LGTM, thanks for all the updates
CB1-2048
What changed? Why?
initialPageprop to the cds-mobileCarouselso consumers can open it on a non-zero page with no animation.activePageIndexat init + a one-time,didInitRef-guarded non-animated seek on first measurement (instant.set(), no slide from page 0); readsinitialPagevia a ref since data can arrive async.onChangePageon mount; clamps out-of-range; routes throughfindNearestLoopOffsetwhen looping; a latergoToPage/drag still wins.requestAnimationFrame+goToPageworkaround (clearsTODO(CB1-2026)); unblocks the RN adoption PR (PR 2).Root cause (required for bugfixes)
N/A — feature, not a bugfix.
UI changes
Adds an "Initial Page (opens on page 3)" example to the mobile Carousel stories. Web unaffected.
CB1-2048-cds.mov
CB1-2048-cds-web.mov
Testing
How has it been tested?
Testing instructions
Automated: New
Initial Pagetests inCarousel.test.tsx— opens oninitialPage;onChangePagenot called on mount; out-of-range clamps (99→6,-5→0); latergoToPageand drag override the initial page. Full file 97/97 pass;mobiletypecheck + lint clean.Manual:
yarn nx run expo-app:ios→ open the Carousel route → the "Initial Page (opens on page 3)" example lands on page 3 with no slide from page 1; dragging/pagination still work.Illustrations/Icons Checklist
N/A — no changes under
packages/illustrations/**orpackages/icons/**.Change management
type=routine
risk=low
impact=sev5
automerge=false